package com.adminpanel.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;

import com.adminpanel.modelbean.ProductBean;
import com.adminpanel.modelbean.VoteBean;
import com.adminpanel.util.Util;

public class VoteServices {
	private JdbcTemplate jdbcTemplate;
	private DataSource dataSource;
	
	@Resource(name = "dataSource")
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	//列举定时任务列表
	public List<VoteBean> getVoteByUserId(int user_id){
		
		String sql = "select * from adminpanel_vote where user_id = ?  order by id desc";
		RowMapper<VoteBean> mapper = new RowMapper<VoteBean>(){

			@Override
			public VoteBean mapRow(ResultSet rs, int rowNum) throws SQLException {
				
				VoteBean votebean = new VoteBean();
				votebean.setId(rs.getInt("id"));
				votebean.setUser_id(rs.getInt("user_id"));
				votebean.setVote_title(rs.getString("vote_title"));
				votebean.setAgree(rs.getInt("agree"));
				votebean.setNeutrally(rs.getInt("neutrally"));
				votebean.setDisagree(rs.getInt("disagree"));
				votebean.setStart_time(rs.getString("start_time"));
				votebean.setEnd_time(rs.getString("end_time"));
				return votebean;
				
			}
			
		};

		return jdbcTemplate.query(sql, mapper, new Object[]{user_id});
		
		
	}
	
	//增加定时任务列表
	
	public void addVoteTask(final VoteBean votebean){
		
		jdbcTemplate.update("insert into adminpanel_vote values(?,?,?,?,?,?,?,?)",
				new PreparedStatementSetter() {

					
					@Override
					public void setValues(PreparedStatement ps) {
						
						try {
							ps.setInt(1, 0);
							ps.setInt(2, votebean.getUser_id());
							ps.setString(3, votebean.getVote_title());
							ps.setInt(4, 0);
							ps.setInt(5, 0);
							ps.setInt(6, 0);
							ps.setString(7, "not set");
							ps.setString(8, "not set");
							
						} catch (SQLException e) {
							

							e.printStackTrace();
						}
					}

				});
	}
}
